Namespace("org.wattdepot.gdata.kukuicup");

// Configuration values for the Kukui Cup.
// Use 'new' with this just in case we extend in future with updatable state. 
org.wattdepot.gdata.kukuicup.Configuration = function() {

  // Source identifiers. 
  var ilima = "Ilima";
  var ilima_3_4 = "Ilima 3-4";
  var ilima_5_6 = "Ilima 5-6";
  var ilima_7_8 = "Ilima 7-8";
  var ilima_9_10 = "Ilima 9-10";
  var ilima_11_12 = "Ilima 11-12";
  var lehua = "Lehua";
  var lehua_3_4 = "Lehua 3-4";
  var lehua_5_6 = "Lehua 5-6";
  var lehua_7_8 = "Lehua 7-8";
  var lehua_9_10 = "Lehua 9-10";
  var lehua_11_12 = "Lehua 11-12";
  var mokihana = "Mokihana";
  var mokihana_3_4 = "Mokihana 3-4";
  var mokihana_5_6 = "Mokihana 5-6";
  var mokihana_7_8 = "Mokihana 7-8";
  var mokihana_9_10 = "Mokihana 9-10";
  var mokihana_11_12 = "Mokihana 11-12";

  // Color scheme
  var widgetBackgroundColor = '#F5F3E5'; // tan
  var widgetHeaderColor = '#459E00'; // light green
  var widgetHeaderTextColor = 'white'; // this is a guess.
  var widgetTextColor = '#312E25'; // near black
  var windowNavBarColor = '#2F6B00'; // dark green

  var greenIndicatorColor =   '#459E00';
  var yellowIndicatorColor = '#CCCC00';
  var redIndicatorColor = '#FF0000';
  var grayIndicatorColor = '#C0C0C0';

  // Widths and Heights
  var energyStatusSummaryWidth = 150;
  var barChartWidth = 300;
  var barChartHeight = 150;
  var standingsWidth = 260;
  var gaugeWidth = 90;
  var powerMonitorWidth = 250;
  var hotSpotChartWidth = 500;
  var hotSpotChartHeight = 200;

  // Images
  var smileySadUrl = 'http://wattdepot-gdata.googlecode.com/svn/trunk/javascript/smiley.sad.png';
  var smileyHappyUrl = 'http://wattdepot-gdata.googlecode.com/svn/trunk/javascript/smiley.happy.png';
  var smileyIndifferentUrl = 'http://wattdepot-gdata.googlecode.com/svn/trunk/javascript/smiley.indifferent.png';

  ///////////////////////////////////////////////////////////////////
  // DEPRECATED: Gets the spreadsheet containing sample annotated timeline data.
  var getSampleAnnotatedTimelineUrl = function() {
    return 'http://spreadsheets.google.com/tq?key=0An9ynmXUoikYdHZKbFI4Sm1id0Z4aVVoRnhkczh5RlE&range=A1:C760&gid=0';
  }

  ///////////////////////////////////////////////////////////////////
  // Gets the spreadsheet containing Kukui Cup Individual Point Standings data.
  // Spreadsheet name: KukuiCupIndividualPointStandings
  var getIndividualPointStandingsUrl = function() {
    return 'http://spreadsheets.google.com/tq?key=0An9ynmXUoikYdF9FekJDSkppOEczZy03ZUt1MUxVYVE&range=A1:E69&gid=0';
  }

  ///////////////////////////////////////////////////////////////////
  // Gets the spreadsheet containing sparkline data for all sources.
  // Spreadsheet name: KukuiCupEnergySummaryData
  var getEnergySummaryDataUrl = function() {
    return 'http://spreadsheets.google.com/tq?key=0An9ynmXUoikYdDBsb3U5dXRIQ24wblpPOUZoVmJpTnc&range=A1:BT26&gid=0';
  }

  ///////////////////////////////////////////////////////////////////
  // Create a map from source names to short (lounge) names
  var sourceToShortMap = {};
  sourceToShortMap[ilima_3_4] = 'Ilima Lounge A';
  sourceToShortMap[ilima_5_6] = 'Ilima Lounge B';
  sourceToShortMap[ilima_7_8] = 'Ilima Lounge C';
  sourceToShortMap[ilima_9_10] = 'Ilima Lounge D';
  sourceToShortMap[ilima_11_12] = 'Ilima Lounge E';
  sourceToShortMap[mokihana_3_4] = 'Mokihana Lounge A';
  sourceToShortMap[mokihana_5_6] = 'Mokihana Lounge B';
  sourceToShortMap[mokihana_7_8] = 'Mokihana Lounge C';
  sourceToShortMap[mokihana_9_10] = 'Mokihana Lounge D';
  sourceToShortMap[mokihana_11_12] = 'Mokihana Lounge E';
  sourceToShortMap[lehua_3_4] = 'Lehua Lounge A';
  sourceToShortMap[lehua_5_6] = 'Lehua Lounge B';
  sourceToShortMap[lehua_7_8] = 'Lehua Lounge C';
  sourceToShortMap[lehua_9_10] = 'Lehua Lounge D';
  sourceToShortMap[lehua_11_12] = 'Lehua Lounge E';
  var getSourceToShort = function(source) {
    return sourceToShortMap[source];
  }


  ///////////////////////////////////////////////////////////////////
  // Create the column map that indicates the start column for a given source
  var energySummaryStartColumnMap = {};
  energySummaryStartColumnMap[ilima] = 0;
  energySummaryStartColumnMap[ilima_3_4] = 8;
  energySummaryStartColumnMap[ilima_5_6] = 16;
  energySummaryStartColumnMap[ilima_7_8] = 24;
  energySummaryStartColumnMap[ilima_9_10] = 32;
  energySummaryStartColumnMap[ilima_11_12] = 40;
  energySummaryStartColumnMap[mokihana] = 48;
  energySummaryStartColumnMap[mokihana_3_4] = 56;
  energySummaryStartColumnMap[mokihana_5_6] = 64;
  energySummaryStartColumnMap[mokihana_7_8] = 72;
  energySummaryStartColumnMap[mokihana_9_10] = 80;
  energySummaryStartColumnMap[mokihana_11_12] = 88;
  energySummaryStartColumnMap[lehua] = 96;
  energySummaryStartColumnMap[lehua_3_4] = 104;
  energySummaryStartColumnMap[lehua_5_6] = 112;
  energySummaryStartColumnMap[lehua_7_8] = 120;
  energySummaryStartColumnMap[lehua_9_10] = 128;
  energySummaryStartColumnMap[lehua_11_12] = 136;
  var getEnergySummaryStartColumn = function(source) {
    return energySummaryStartColumnMap[source];
  }

  var energySummaryLastHrColumnOffset = 1;
  var energySummaryLastDayColumnOffset = 2;
  var energySummaryLastWeekColumnOffset = 3;
  var energySummaryDateColumnOffset = 4;
  var energySummaryCumulativeEnergyColumnOffset = 5;
  var energySummaryCumulativeBaselineColumnOffset = 6;
  var energySummaryLastUpdateColumnOffset = 7;

  ///////////////////////////////////////////////////////////////////
  // Gets the spreadsheet containing latest power readings for all sources.
  // Spreadsheet name: KukuiCupData
  var getCurrentPowerDataUrl = function() {
    return 'http://spreadsheets.google.com/tq?key=0Av0U6TKHfzXYdG1vUnduR0RVTktyR1ZtNjAtSE9Qbmc&range=A3:F21&gid=0';
  }
  // Create the row map for current Power data and the access function.
  var currentPowerDataRowMap = {};
  currentPowerDataRowMap[ilima_3_4] = 0;
  currentPowerDataRowMap[ilima_5_6] = 1;
  currentPowerDataRowMap[ilima_7_8] = 2;
  currentPowerDataRowMap[ilima_9_10] = 3;
  currentPowerDataRowMap[ilima_11_12] = 4;
  currentPowerDataRowMap[ilima] = 5;
  currentPowerDataRowMap[mokihana_3_4] = 6;
  currentPowerDataRowMap[mokihana_5_6] = 7;
  currentPowerDataRowMap[mokihana_7_8] = 8;
  currentPowerDataRowMap[mokihana_9_10] = 9;
  currentPowerDataRowMap[mokihana_11_12] = 10;
  currentPowerDataRowMap[mokihana] = 11;
  currentPowerDataRowMap[lehua_3_4] = 12;
  currentPowerDataRowMap[lehua_5_6] = 13;
  currentPowerDataRowMap[lehua_7_8] = 14;
  currentPowerDataRowMap[lehua_9_10] = 15;
  currentPowerDataRowMap[lehua_11_12] = 16;
  currentPowerDataRowMap[lehua] = 17;
  var getCurrentPowerDataRow = function(source) {
    return currentPowerDataRowMap[source];
  }

  var currentPowerDataNameColumn = 0;
  var currentPowerDataPowerColumn = 2;
  var currentPowerDataLastUpdateColumn = 3;

  ///////////////////////////////////////////////////////////////////
  // Gets the spreadsheet containing cumulative energy for all sources. Currently the same as current power.
  // Spreadsheet name: KukuiCupData
  var getCumulativeEnergyDataUrl = function() {
    return 'http://spreadsheets.google.com/tq?key=0Av0U6TKHfzXYdG1vUnduR0RVTktyR1ZtNjAtSE9Qbmc&range=A3:F21&gid=0';
  }
  // Create the row map for cumulative energy data and the access function.
  var cumulativeEnergyDataRowMap = {};
  cumulativeEnergyDataRowMap[ilima_3_4] = 0;
  cumulativeEnergyDataRowMap[ilima_5_6] = 1;
  cumulativeEnergyDataRowMap[ilima_7_8] = 2;
  cumulativeEnergyDataRowMap[ilima_9_10] = 3;
  cumulativeEnergyDataRowMap[ilima_11_12] = 4;
  cumulativeEnergyDataRowMap[ilima] = 5;
  cumulativeEnergyDataRowMap[mokihana_3_4] = 6;
  cumulativeEnergyDataRowMap[mokihana_5_6] = 7;
  cumulativeEnergyDataRowMap[mokihana_7_8] = 8;
  cumulativeEnergyDataRowMap[mokihana_9_10] = 9;
  cumulativeEnergyDataRowMap[mokihana_11_12] = 10;
  cumulativeEnergyDataRowMap[mokihana] = 11;
  cumulativeEnergyDataRowMap[lehua_3_4] = 12;
  cumulativeEnergyDataRowMap[lehua_5_6] = 13;
  cumulativeEnergyDataRowMap[lehua_7_8] = 14;
  cumulativeEnergyDataRowMap[lehua_9_10] = 15;
  cumulativeEnergyDataRowMap[lehua_11_12] = 16;
  cumulativeEnergyDataRowMap[lehua] = 17;
  var getCumulativeEnergyDataRow = function(source) {
    return cumulativeEnergyDataRowMap[source];
  }


  ///////////////////////////////////////////////////////////////////
  // Get the baseline data URL.
  // Spreadsheet name: KukuiCupBaselineData
  var getBaselineDataUrl = function() {
    return 'http://spreadsheets.google.com/tq?key=0An9ynmXUoikYdG15dlp1aUZEVzdDT283ZFd5cUdkUnc&range=A1:Y19&gid=0';
  }
  // Create the row map for baseline data and the access function.
  var baselineDataRowMap = {};
  baselineDataRowMap[lehua] = 0;
  baselineDataRowMap[lehua_3_4] = 1;
  baselineDataRowMap[lehua_5_6] = 2;
  baselineDataRowMap[lehua_7_8] = 3;
  baselineDataRowMap[lehua_9_10] = 4;
  baselineDataRowMap[lehua_11_12] = 5;
  baselineDataRowMap[ilima] = 6;
  baselineDataRowMap[ilima_3_4] = 7;
  baselineDataRowMap[ilima_5_6] = 8;
  baselineDataRowMap[ilima_7_8] = 9;
  baselineDataRowMap[ilima_9_10] = 10;
  baselineDataRowMap[ilima_11_12] = 11;
  baselineDataRowMap[mokihana] = 12;
  baselineDataRowMap[mokihana_3_4] = 13;
  baselineDataRowMap[mokihana_5_6] = 14;
  baselineDataRowMap[mokihana_7_8] = 15;
  baselineDataRowMap[mokihana_9_10] = 16;
  baselineDataRowMap[mokihana_11_12] = 17;
  var getBaselineDataRow = function(source) {
    return baselineDataRowMap[source];
  }

  // Return the column in the baselineData spreadsheet providing the hourly baseline for right now.
  var getBaselineDataColumn = function() {
    var currTime = new Date();
    var hourOfDay = currTime.getHours();
    return hourOfDay + 2;
  }

  // Return the color to display based upon comparing the power to its baseline.
  // Red if 50% over baseline, green if less than 10% over baseline, yellow otherwise.
  var getBaselineDataColor = function(power, baseline) {
    // Convert to a number because internal commas mess up comparisons.
    power = parseInt(power.replace(',', ''));
    baseline = parseInt(baseline.replace(',', ''));
    if (power > (baseline * 1.5)) {
      return redIndicatorColor;
    }
    else if (power < (baseline * 1.1)) {
      return greenIndicatorColor;
    }
    else {
      return yellowIndicatorColor;
    }
  }

  // Return a string indicating the percentage difference between the baseline and the power.
  var getBaselineDataComment = function(power, baseline) {
    // Convert to a number because internal commas mess up comparisons.
    power = parseInt(power.replace(',', ''));
    baseline = parseInt(baseline.replace(',', ''));
    if (power === baseline) {
      return 'Right on the baseline!';
    }
    var over = (power > baseline);
    if (over) {
      var amountOver = power - baseline;
      var percentOver = parseInt((amountOver / baseline) * 100);
      return percentOver + '% over baseline';
    }
    else { 
      var amountUnder = baseline - power;
      var percentUnder = parseInt((amountUnder / baseline) * 100);
      return percentUnder + '% under baseline';
    }
  }

  ///////////////////////////////////////////////////////////////////
  // Get the Rolling Seconds Power URL.
  // Spreadsheet name: KukuiCupRollingSecondsPowerNew
  var getRollingSecondsPowerUrl = function() {
    return 'https://spreadsheets.google.com/tq?key=0An9ynmXUoikYdEVPRVJLMk5qbHBMTzhGVHBRMXJuZFE';
  } 

  ///////////////////////////////////////////////////////////////////
  // Get the Rolling Hours Energy URL.
  // Spreadsheet name: KukuiCupRollingHoursEnergyNew
  var getRollingHoursEnergyUrl = function() {
    return 'https://spreadsheets.google.com/tq?key=0An9ynmXUoikYdGRMdjdkOG5WUFN1amdXRC01ejNzRFE';
  }

  ///////////////////////////////////////////////////////////////////
  // Get the Rolling Days Energy URL.
  // Spreadsheet name: KukuiCupRollingDaysEnergyNew
  var getRollingDaysEnergyUrl = function() {
    return 'https://spreadsheets.google.com/tq?key=0An9ynmXUoikYdG04UXZHdTFZQVhzeGtPTDF3S1lheUE';
    // more recent: 'https://spreadsheets.google.com/tq?key=0An9ynmXUoikYdFNCcklkNURhblVsMFEyTkJLV1dLVGc'
    // (old)'https://spreadsheets.google.com/tq?range=A2:AY18&key=0An9ynmXUoikYdGRMdjdkOG5WUFN1amdXRC01ejNzRFE&gid=0';
  }
  
  // Create the row map for both rolling hours and days spreadsheets.
  var rollingEnergyRowMap = {};
  rollingEnergyRowMap[lehua] = 18;
  rollingEnergyRowMap[lehua_3_4] = 13;
  rollingEnergyRowMap[lehua_5_6] = 14;
  rollingEnergyRowMap[lehua_7_8] = 15;
  rollingEnergyRowMap[lehua_9_10] = 16;
  rollingEnergyRowMap[lehua_11_12] = 17;
  rollingEnergyRowMap[ilima] = 6;
  rollingEnergyRowMap[ilima_3_4] = 1;
  rollingEnergyRowMap[ilima_5_6] = 2;
  rollingEnergyRowMap[ilima_7_8] = 3;
  rollingEnergyRowMap[ilima_9_10] = 4;
  rollingEnergyRowMap[ilima_11_12] = 5;
  rollingEnergyRowMap[mokihana] = 12;
  rollingEnergyRowMap[mokihana_3_4] = 7;
  rollingEnergyRowMap[mokihana_5_6] = 8;
  rollingEnergyRowMap[mokihana_7_8] = 9;
  rollingEnergyRowMap[mokihana_9_10] = 10;
  rollingEnergyRowMap[mokihana_11_12] = 11;
  var getRollingEnergyRow = function(source) {
    return rollingEnergyRowMap[source];
  }
  
  // Returns the passed date string in yyyy/mm/dd hh:mm:ss format.
  function toShortDate(date) {
    var tmp =
    (date.getMonth() + 1) + '/' +
    date.getDate() + '/' +
    date.getFullYear() + ' ' +
    date.getHours() + ':' +
    (date.getMinutes() < 10 ? '0' : '') +
    date.getMinutes() + ':' +
    (date.getSeconds() < 10 ? '0' : '') +
    date.getSeconds();
    return tmp;
  }


  return {
    // Specify the public interface.
    // Source identifiers
    ilima : ilima,
    ilima_3_4 : ilima_3_4,
    ilima_5_6 : ilima_5_6,
    ilima_7_8 : ilima_7_8,
    ilima_9_10 : ilima_9_10,
    ilima_11_12 : ilima_11_12,
    lehua : lehua,
    lehua_3_4 : lehua_3_4,
    lehua_5_6 : lehua_5_6,
    lehua_7_8 : lehua_7_8,
    lehua_9_10 : lehua_9_10,
    lehua_11_12 : lehua_11_12,
    mokihana : mokihana,
    mokihana_3_4 : mokihana_3_4,
    mokihana_5_6 : mokihana_5_6,
    mokihana_7_8 : mokihana_7_8,
    mokihana_9_10 : mokihana_9_10,
    mokihana_11_12 : mokihana_11_12,
    // Source Name to Short Name Map.
    getSourceToShort : getSourceToShort,
    // Color theme
    widgetBackgroundColor : widgetBackgroundColor,
    widgetHeaderColor : widgetHeaderColor,
    widgetHeaderTextColor : widgetHeaderTextColor,
    widgetTextColor : widgetTextColor,
    windowNavBarColor : windowNavBarColor,
    greenIndicatorColor : greenIndicatorColor,
    yellowIndicatorColor : yellowIndicatorColor,
    redIndicatorColor : redIndicatorColor,
    grayIndicatorColor : grayIndicatorColor,
    // Widths and Heights
    energyStatusSummaryWidth : energyStatusSummaryWidth,
    standingsWidth : standingsWidth,
    barChartWidth : barChartWidth,
    barChartHeight : barChartHeight,
    hotSpotChartWidth : hotSpotChartWidth,
    hotSpotChartHeight : hotSpotChartHeight,
    gaugeWidth : gaugeWidth,
    powerMonitorWidth : powerMonitorWidth,
    // Images
    smileySadUrl : smileySadUrl,
    smileyHappyUrl : smileyHappyUrl,
    smileyIndifferentUrl : smileyIndifferentUrl,
    // EnergySummary data
    getEnergySummaryDataUrl : getEnergySummaryDataUrl,
    getEnergySummaryStartColumn : getEnergySummaryStartColumn,
    energySummaryLastHrColumnOffset :  energySummaryLastHrColumnOffset, 
    energySummaryLastDayColumnOffset : energySummaryLastDayColumnOffset,
    energySummaryLastWeekColumnOffset : energySummaryLastWeekColumnOffset,
    energySummaryDateColumnOffset : energySummaryDateColumnOffset,
    energySummaryCumulativeEnergyColumnOffset : energySummaryCumulativeEnergyColumnOffset,
    energySummaryCumulativeBaselineColumnOffset : energySummaryCumulativeBaselineColumnOffset,
    energySummaryLastUpdateColumnOffset : energySummaryLastUpdateColumnOffset,
    // Baseline Data Spreadsheet
    getBaselineDataUrl : getBaselineDataUrl,
    getBaselineDataRow : getBaselineDataRow,
    getBaselineDataColumn : getBaselineDataColumn,
    getBaselineDataColor : getBaselineDataColor,
    getBaselineDataComment : getBaselineDataComment,
    // Current Power Spreadsheet
    getCurrentPowerDataUrl : getCurrentPowerDataUrl,
    getCurrentPowerDataRow : getCurrentPowerDataRow,
    currentPowerDataNameColumn : currentPowerDataNameColumn,
    currentPowerDataPowerColumn : currentPowerDataPowerColumn,
    currentPowerDataLastUpdateColumn : currentPowerDataLastUpdateColumn,
    // Annotated timeline (DEPRECATED)
    getSampleAnnotatedTimelineUrl : getSampleAnnotatedTimelineUrl,
    // Individual Kukui Cup Point Standings 
    getIndividualPointStandingsUrl : getIndividualPointStandingsUrl,
    // Rolling Hours Energy and Rolling Days Energy
    getRollingHoursEnergyUrl :  getRollingHoursEnergyUrl,
    getRollingDaysEnergyUrl :  getRollingDaysEnergyUrl,
    getRollingSecondsPowerUrl :  getRollingSecondsPowerUrl,
    getRollingEnergyRow :  getRollingEnergyRow,
    // Cumulative Energy Spreadsheet
    getCumulativeEnergyDataUrl : getCumulativeEnergyDataUrl,
    getCumulativeEnergyDataRow : getCumulativeEnergyDataRow,
    toShortDate : toShortDate // No comma
  };
}




